package com.lovo.Atm;

import com.lovo.Stream.SerializeUtil;
import com.lovo.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;

public class Mod extends JPanel implements ActionListener {
    private Home frame;
    private JButton backImg;
    private JButton sureImg;
    private JLabel oldPsw;
    private JLabel newPsw;
    private JLabel confirmPsw;
    private JTextField oldText;
    private JTextField newText;
    private JTextField confirmText;
    private Icon backBtn = Util.iconImg("/images/buttonOff.jpg");
    private Icon sureBtn = Util.iconImg("/images/buttonSure.jpg");

    public Mod(Home frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        //请输入旧密码
        oldPsw = new JLabel("请输入旧密码:");
        oldPsw.setFont(new Font("宋体", Font.BOLD, 20));
        oldPsw.setBounds(300, 200, 150, 30);
        this.add(oldPsw);

        oldText = new JTextField();
        oldText.setFont(new Font("宋体", Font.BOLD, 20));
        oldText.setBounds(470, 200, 100, 30);
        this.add(oldText);

        //请输入新密码
        newPsw = new JLabel("请输入新密码（6位数字）:");
        newPsw.setFont(new Font("宋体", Font.BOLD, 20));
        newPsw.setBounds(300, 250, 150, 30);
        this.add(newPsw);

        newText = new JTextField();
        newText.setFont(new Font("宋体", Font.BOLD, 20));
        newText.setBounds(470, 250, 100, 30);
        this.add(newText);

        //请确认新密码
        confirmPsw = new JLabel("请确认新密码:");
        confirmPsw.setFont(new Font("宋体", Font.BOLD, 20));
        confirmPsw.setBounds(590, 250, 150, 30);
        this.add(confirmPsw);

        confirmText = new JTextField();
        confirmText.setFont(new Font("宋体", Font.BOLD, 20));
        confirmText.setBounds(760, 250, 100, 30);
        this.add(confirmText);

        //确认按钮
        sureImg = new JButton(sureBtn);
        sureImg.setBounds(0, 420, 140, 50);
        this.add(sureImg);
        sureImg.addActionListener(this);

        //退出程序按钮
        backImg = new JButton(backBtn);
        backImg.setBounds(870, 420, 140, 50);
        this.add(backImg);
        backImg.addActionListener(this);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == backImg) {
            frame.switchPanel("menu");
            oldText.setText("");
            newText.setText("");
            confirmText.setText("");
        }
        if (e.getSource() == sureImg) {
            //对比旧密码
            String prPsw = oldText.getText();//输入的旧密码
            String psw = frame.getLoginUser().getPassword();
            String newPsw = newText.getText();//输入的新密码
            String conPsw = confirmText.getText();//确认新密码
            if (prPsw.equals(psw)) {//如果旧密码相同进入下一步
                if (newPsw.matches("^[0-9]{6}$")) {//如果新密码符合规则，下一步
                    if (conPsw.equals(newPsw)){ //两次输入密码相同
                        frame.getLoginUser().setPassword(newPsw);
                        SerializeUtil.writeObject(Home.path,Home.user);
                        JOptionPane.showMessageDialog(this,"修改密码成功，请重新登录！");
                        oldText.setText("");
                        newText.setText("");
                        confirmText.setText("");
                        frame.switchPanel("land");
                    }else{
                        JOptionPane.showMessageDialog(this,"确认新密码有误，请重新输入！");
                        confirmText.setText("");
                    }
                }else{
                    JOptionPane.showMessageDialog(this,"新密码不符合格式，请重新输入！");
                    confirmText.setText("");
                    newText.setText("");
                }
            } else {
                JOptionPane.showMessageDialog(this, "输入旧密码有误，请重新输入！");
                oldText.setText("");
                newText.setText("");
                confirmText.setText("");
            }
        }


    }
}

